package com.hu.common.util.adm;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author 355760
 * @ClassName TimeFormatUtil
 * @Description 时间格式化工具类
 */
public class TimeFormatUtil {
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");
    private static final List<String> WEEK_NAME_LIST = Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
    private static final List<String> MONTH_NAME_LIST = Arrays.asList("Jan", "Feb", "Mar", "Apr", "May", "June", "Jul",
            "Aug", "Sep", "Oct", "Nove", "December");


    public static String getDateTimeStr(LocalDateTime localDateTime) {
        if (Assert.notEmpty(localDateTime)) {
            return localDateTime.format(DATE_TIME_FORMATTER);
        }
        return null;
    }

    public static String getMonthStr(LocalDateTime localDateTime) {
        if (Assert.notEmpty(localDateTime)) {
            return localDateTime.format(MONTH_FORMATTER);
        }
        return null;
    }

    public static String getDateStr(LocalDate localDate) {
        if (Assert.notEmpty(localDate)) {
            return localDate.format(DATE_FORMATTER);
        }
        return null;
    }

    /**
     * 通过formatter获取指定格式的时间字符串
     *
     * @param localDateTime     时间
     * @param dateTimeFormatter 时间格式化对象
     * @return 时间字符串
     */
    public static String getFormatStrWithFormatter(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {
        if (Assert.notEmpty(localDateTime)) {
            return localDateTime.format(dateTimeFormatter);
        }
        return null;
    }

    public static String getNowFormatStrWithFormatter(DateTimeFormatter dateTimeFormatter) {
        return getFormatStrWithFormatter(LocalDateTime.now(), dateTimeFormatter);
    }

    /**
     * 日期字符串转LocalDateTime
     *
     * @param dateStr 日期字符串
     * @return LocalDateTime
     */
    public static LocalDate parseDateStr(String dateStr) {
        return LocalDate.parse(dateStr, DATE_FORMATTER);
    }

    /**
     * 日期时间字符串转LocalDateTime
     *
     * @param dateTimeStr 日期时间字符串
     * @return LocalDateTime
     */
    public static LocalDateTime parseDateTimeStr(String dateTimeStr) {
        return LocalDateTime.parse(dateTimeStr, DATE_TIME_FORMATTER);
    }

    /**
     * LocalDateTime转Date
     *
     * @param localDateTime LocalDateTime
     * @return Date
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * Date转LocalDateTime
     *
     * @param date Date
     * @return LocalDateTime
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    public static String getNowWeekName(LocalDateTime localDateTime) {
        return WEEK_NAME_LIST.get(localDateTime.getDayOfWeek().getValue() - 1);
    }

    public static String getNowMonthName(LocalDateTime localDateTime) {
        return MONTH_NAME_LIST.get(localDateTime.getMonth().getValue() - 1);
    }

    /**
     * 返回某个日期前几天的日期
     *
     * @param date
     * @param i
     * @return Date
     */
    public static Date getFrontDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
        return cal.getTime();
    }
}
